package de.tu_darmstadt.gdi1.xcom.tests.testcases.students;

import static org.junit.Assert.*;

import org.junit.Before;
import org.junit.Test;

import de.tu_darmstadt.gdi1.xcom.tests.adapters.XcomTestAdapterLevel1;

/**
 * 
 * Tests at the end of one's turn all data is correctly reset
 * 
 * @author Lukas Schwoebel
 *
 */
public class EndTurnTests {
	
	private XcomTestAdapterLevel1 testAdapter;
	
	/**
	 * @throws java.lang.Exception
	 */
	@Before
	public void setUp() throws Exception {
		testAdapter = new XcomTestAdapterLevel1();
	}

	/**
	 * Test method for {@link de.tu_darmstadt.gdi1.xcom.tests.adapters.XcomTestAdapterLevel1#endTurn()}.
	 */
	@Test
	public final void testsActionPointsReset() {
		testAdapter.loadLevelFromString(TestLevel.testLevel);

		// first do some actions to reduce action-points
		testAdapter.rotateActivePlayer(false);
		testAdapter.rotateActivePlayer(true);
		testAdapter.rotateActivePlayer(true);
		testAdapter.rotateActivePlayer(true);
		testAdapter.rotateActivePlayer(false);
		testAdapter.rotateActivePlayer(false);
		testAdapter.rotateActivePlayer(true);
		// now player should have less actionpoints
		assertEquals("After 7 rotations only 68 actionpoints should be left", 68, testAdapter.actionPointsLeft());
		
		// change player and do some acions
		testAdapter.changeActivePlayer(2, 5);
		testAdapter.rotateActivePlayer(true);
		testAdapter.rotateActivePlayer(true);
		testAdapter.rotateActivePlayer(true);
		testAdapter.rotateActivePlayer(true);
		testAdapter.rotateActivePlayer(false);
		testAdapter.rotateActivePlayer(false);
		testAdapter.rotateActivePlayer(true);
		testAdapter.rotateActivePlayer(false);
		// now player should have less actionpoints
		assertEquals("After 8 rotations only 67 should be left", 67, testAdapter.actionPointsLeft());
		
		// now finish turn and check if actionpoints were reset
		testAdapter.endTurn();
		
		testAdapter.changeActivePlayer(4, 2);
		assertEquals("After ending the turn, the actionpoints should be reset to 75", TestLevel.actionPoints, testAdapter.actionPointsLeft());
		testAdapter.changeActivePlayer(2, 5);
		assertEquals("After ending the turn, the actionpoints should be reset to 75", TestLevel.actionPoints, testAdapter.actionPointsLeft());
		// all other players should have 75, too (well, there is only one left)
		testAdapter.changeActivePlayer(4, 6);
		assertEquals("After ending the turn, the actionpoints should be reset to 75", TestLevel.actionPoints, testAdapter.actionPointsLeft());
	}

	/**
	 * Test method for {@link de.tu_darmstadt.gdi1.xcom.tests.adapters.XcomTestAdapterLevel1#endTurn()}.
	 */
	@Test
	public final void testPersistantStateAfterEndTurn() {
		testAdapter.loadLevelFromString(TestLevel.testLevel);

		// as in the first test the location of the humans didn't change, we will test this first
		
		testAdapter.changeActivePlayer(4, 2);
		testAdapter.performActionForActivePlayer(); // move to 4:1
		testAdapter.rotateActivePlayer(false);
		testAdapter.performActionForActivePlayer(); // move to 3:1
		testAdapter.performActionForActivePlayer(); // move to 2:1
		testAdapter.endTurn();
		
		// now choose another player
		testAdapter.changeActivePlayer(2, 5);
		// try to change to player 4:2 - should not be possible
		testAdapter.changeActivePlayer(4, 2);
		// turn and move player, should move 2:5!
		testAdapter.rotateActivePlayer(false);
		testAdapter.performActionForActivePlayer(); // move to 1:5
		
		String testLevelString1 = "" +
				"############\n" +
				"##@..#######\n" +
				"##ZZ.##...##\n" +
				"##..###Z..##\n" +
				"###.####.###\n" +
				"#@...##Z..##\n" +
				"#...@....Z##\n" +
				"############";
		String testLevelString2 = "" +
				"############\n" +
				"##@..#######\n" +
				"##ZZ.##...##\n" +
				"##..###Z..##\n" +
				"#c#.####=###\n" +
				"#@...##Z..##\n" +
				"#...@....Z##\n" +
				"############";
		
		String levelStringAdapter = testAdapter.levelRepresentationAsString();
		boolean testStringIsEqual = (levelStringAdapter.equals(testLevelString1)) || (levelStringAdapter.equals(testLevelString2));
		
		// check if right player moved
		assertTrue("After endturn, the players positions should be updated/kept, player 4:2 doesnt exist any more (wrong player was moved)\n" +
				"Should be\n" + testLevelString1 + ", was\n" + testAdapter.levelRepresentationAsString(), testStringIsEqual);
		
		// now kill zombie with player at 2:1 and end turn
		testAdapter.changeActivePlayer(2, 1);
		testAdapter.rotateActivePlayer(false);
		testAdapter.performActionForActivePlayer();
		// zombie was killed?
		assertEquals("The zombie at 2:2 should be killed:\n"+testAdapter.levelRepresentationAsString(), '.', testAdapter.elementOnPosition(2, 2));
		testAdapter.endTurn();
		// zombie still dead?
		assertEquals("The zombie at 2:2 should be still dead after end turn:\n"+testAdapter.levelRepresentationAsString(), '.', testAdapter.elementOnPosition(2, 2));
	}	
}
